package com.genia.toolbox.persistence.hibernate.spring.factory;

import javax.transaction.UserTransaction;

import org.hibernate.transaction.JTATransactionFactory;
import org.springframework.orm.hibernate3.LocalSessionFactoryBean;

/**
 * {@link JTATransactionFactory} that can returns the
 * {@link javax.transaction.TransactionManager} configured by spring when it
 * implements the correct interface. TODO Write description
 */
public class SpringJTATransactionFactory
    extends JTATransactionFactory
{
  /**
   * the {@link UserTransaction} to return.
   */
  private transient UserTransaction userTransaction;



  /**
   * constructor.
   */
  public SpringJTATransactionFactory()
  {
    Object userTransaction = LocalSessionFactoryBean.getConfigTimeTransactionManager();
    if (userTransaction == null || !(userTransaction instanceof UserTransaction)) {
      this.userTransaction = super.getUserTransaction();
    }
    else {
      this.userTransaction = (UserTransaction) userTransaction;
    }
  }



  /**
   * Get the {@link UserTransaction} reference.
   * 
   * @return The appropriate {@link UserTransaction} reference
   * @see org.hibernate.transaction.JTATransactionFactory#getUserTransaction()
   */
  @Override
  protected UserTransaction getUserTransaction()
  {
    return userTransaction;

  }

}
